import 'dart:io';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:path_provider/path_provider.dart';

class FFPath {
  // ..包名/files 结尾不带/
  static Future<String> getPhoneLocalPath() async {
    if (Platform.isIOS) {
      final directory = await getApplicationDocumentsDirectory();
      return directory.path;
    } else {
      // final directory = await getApplicationSupportDirectory();
      // return directory.path;
      final directory = await getExternalStorageDirectory();
      return directory!.path;
    }
  }

  // iOS cache
  static Future<String> getPhoneCachePath() async {
    if (Platform.isIOS) {
      final directory = await getApplicationCacheDirectory();
      return directory.path;
    } else {
      final directory = await getApplicationCacheDirectory();
      return directory.path;
    }
  }

  // ..包名/files 结尾不带/
  static Future<String> getPhoneTmpPath() async {
    if (Platform.isIOS) {
      final directory = await getApplicationCacheDirectory();
      return directory.path;
    } else {
      // final directory = await getApplicationSupportDirectory();
      // return directory.path;
      assert(false, "Android 未实现");
      return "";
    }
  }


  //获取文件名,带后缀
  static String getFileName(String filePath) {
    return RegExp(r'[^/]+$').stringMatch(filePath) ?? '';
  }

  // 获取文件后缀
  static String getFileExtension(String filePath, {bool withDot = true}) {
    String fileName = getFileName(filePath);
    int dotIndex = fileName.lastIndexOf('.');
    if (dotIndex == -1) {
      return ''; // 无后缀
    }

    String dotStr = fileName.substring(dotIndex);
    if (!withDot) return dotStr;

    return dotStr.substring(1);
  }

  /**
   * @title 获取没有后缀的文件名
   * @info
   * @param removeBrackets 是否移除最后的面插号，比如：xxxx(123)
   */
  static String getFileNameWithoutExtension(String filePath, {bool removeBrackets = false,}) {
    String fileName = getFileName(filePath);
    int dotIndex = fileName.lastIndexOf('.');
    String newFileName = "";
    if (dotIndex == -1) {
      newFileName = fileName;
    } else {
      newFileName = fileName.substring(0, dotIndex);
    }

    dotIndex = newFileName.lastIndexOf("(");
    if (dotIndex == -1) {
      return newFileName;
    } else {
      return newFileName.substring(0, dotIndex);
    }
  }

  //求 文件所在的目录
  static String getFileAtDirection(String filePath) {
    if (FFEmpty.stringIsEmpty(filePath)) return "";
    return filePath.substring(0, filePath.lastIndexOf('/'));
  }
}
